#include "editorexitdialog.h"

EditorExitDialog::EditorExitDialog(QString tabName, QWidget *parent) :
    QDialog(parent)
{
    exitDialogLayout_ = new QGridLayout(this);

    exitDialogButtons_ = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel | QDialogButtonBox::Discard);

    exitDialogTopLabel_ = new QLabel("Do you want to discard changes on document \"" + tabName + "\"?", this);
    QFont font = exitDialogTopLabel_->font();
    font.setBold(true);
    font.setPixelSize(15);
    exitDialogTopLabel_->setFont(font);

    exitDialogBottomLabel_ = new QLabel("If you choose to exit without saving, you will lose any unsaved changes.", this);

    exitDialogIcon_ = new QLabel(this);
    QIcon icon = QIcon::fromTheme("dialog-warning");
    exitDialogIcon_->setPixmap(icon.pixmap(48,48));

    exitDialogLayout_->addWidget(exitDialogIcon_, 0, 0, 2, 1);
    exitDialogLayout_->addWidget(exitDialogTopLabel_, 0, 1, 1, 1);
    exitDialogLayout_->addWidget(exitDialogBottomLabel_, 1, 1, 1, 1);
    exitDialogLayout_->addWidget(exitDialogButtons_, 2, 1, 1, 1);

    connect(exitDialogButtons_, SIGNAL(clicked(QAbstractButton*)), this, SLOT(onClicked(QAbstractButton*)));
}

EditorExitDialog::~EditorExitDialog()
{
    delete exitDialogIcon_;
    delete exitDialogTopLabel_;
    delete exitDialogBottomLabel_;
    delete exitDialogButtons_;
    delete exitDialogLayout_;
}

EditorExitDialog::ExitCode EditorExitDialog::exec()
{
    int exit_code = QDialog::exec();

    if(exit_code == Save)
        return Save;
    else if(exit_code == Cancel)
        return Cancel;
    else
        return Discard;
}

void EditorExitDialog::onClicked(QAbstractButton *button)
{
    if(exitDialogButtons_->buttonRole(button) == QDialogButtonBox::AcceptRole)
        QDialog::done(Save);
    else if(exitDialogButtons_->buttonRole(button) == QDialogButtonBox::RejectRole)
        QDialog::done(Cancel);
    else if(exitDialogButtons_->buttonRole(button) == QDialogButtonBox::DestructiveRole)
        QDialog::done(Discard);
}
